package com.chad.baserecyclerviewadapterhelper;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.chad.baserecyclerviewadapterhelper.adapter.FetchLoadAdapter;
import com.chad.baserecyclerviewadapterhelper.data.DataServer;
import com.chad.baserecyclerviewadapterhelper.entity.Status;
import com.chad.library.adapter.base.BaseFetchLoadAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;

import java.util.Collections;
import java.util.List;


/**
 * https://github.com/CymChad/BaseRecyclerViewAdapterHelper
 */
public class PullToRefreshUseActivity2 extends Activity implements BaseFetchLoadAdapter.RequestLoadMoreListener, BaseFetchLoadAdapter.RequestFetchMoreListener {
    private static final int FIRST_LOAD_DATA_COUNT = 10;
    private static final int FETCH_LOAD_DATA_COUNT = 5;
    private static final int FETCH_LOAD_DELAY = 300;
    private RecyclerView mRecyclerView;
    private FetchLoadAdapter mAdapter;
    private boolean mockFetchFailed = false;
    private boolean mockLoadFailed = true;

    // empty
    private View loadingView;
    private View notDataView;
    private View errorView;
    private boolean mError = true;
    private boolean mNoData = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        mRecyclerView = (RecyclerView) findViewById(R.id.rv_list);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        initEmptyView();
        initAdapter();
        initItemClick();
        onRefresh();
    }

    @Override
    public void onLoadMoreRequested() {
        mRecyclerView.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (mockLoadFailed) {
                    mockLoadFailed = false;
                    mAdapter.loadMoreFail();
                    return;
                }

                List<Status> data = DataServer.getSampleDataEx(FETCH_LOAD_DATA_COUNT);
                if (data == null || data.isEmpty()) {
                    mAdapter.loadMoreEnd(false);
                } else {
                    mAdapter.loadMoreComplete(data);
                }
            }
        }, FETCH_LOAD_DELAY);
    }

    @Override
    public void onFetchMoreRequested() {
        mRecyclerView.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (mockFetchFailed) {
                    mockFetchFailed = false;
                    mAdapter.fetchMoreFailed();
                    return;
                }

                List<Status> data = DataServer.getSampleDataEx(FETCH_LOAD_DATA_COUNT);
                Collections.reverse(data);
                if (data == null || data.isEmpty()) {
                    mAdapter.fetchMoreEnd(true);
                } else {
                    mAdapter.fetchMoreComplete(mRecyclerView, data);
                }
            }
        }, FETCH_LOAD_DELAY);
    }

    private void initAdapter() {
        mAdapter = new FetchLoadAdapter(FIRST_LOAD_DATA_COUNT);
        mAdapter.setOnFetchMoreListener(this);
        mAdapter.setOnLoadMoreListener(this);
        mAdapter.openLoadAnimation(BaseFetchLoadAdapter.ALPHAIN);
        mAdapter.setAnimationDuration(50);
        mAdapter.setAutoFetchMoreSize(1);
        mAdapter.setAutoLoadMoreSize(1);
        mRecyclerView.setAdapter(mAdapter);
    }

    private void initEmptyView() {
        loadingView = getLayoutInflater().inflate(R.layout.loading_view, (ViewGroup) mRecyclerView.getParent(), false);
        notDataView = getLayoutInflater().inflate(R.layout.empty_view, (ViewGroup) mRecyclerView.getParent(), false);
        notDataView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onRefresh();
            }
        });
        errorView = getLayoutInflater().inflate(R.layout.error_view, (ViewGroup) mRecyclerView.getParent(), false);
        errorView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onRefresh();
            }
        });
    }

    private void initItemClick() {
        mRecyclerView.addOnItemTouchListener(new OnItemClickListener<BaseFetchLoadAdapter>() {
            @Override
            public void SimpleOnItemClick(BaseFetchLoadAdapter adapter, View view, int position) {
                Status data = mAdapter.getItem(position);

                Toast.makeText(PullToRefreshUseActivity2.this, "" + Integer.toString(position) + "-" + data.getUserName(),
                        Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onItemChildClick(BaseFetchLoadAdapter adapter, View view, int position) {
                super.onItemChildClick(adapter, view, position);
                switch (view.getId()) {
                    case R.id.tweetAvatar:
                        Toast.makeText(PullToRefreshUseActivity2.this, "The " + Integer.toString(position) + " tweetAvatar  is clicked", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.tweetName:
                        Toast.makeText(PullToRefreshUseActivity2.this, "The " + Integer.toString(position) + " tweetName  is clicked", Toast.LENGTH_SHORT).show();
                        break;
                    default:
                        break;
                }
            }

            @Override
            public void onItemLongClick(BaseFetchLoadAdapter adapter, View view, int position) {
                super.onItemLongClick(adapter, view, position);
                Toast.makeText(PullToRefreshUseActivity2.this, "The " + Integer.toString(position) + " Item is LongClick ", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onItemChildLongClick(BaseFetchLoadAdapter adapter, View view, int position) {
                super.onItemChildLongClick(adapter, view, position);
                Toast.makeText(PullToRefreshUseActivity2.this, "The " + Integer.toString(position) + "  view itemchild " + "is LongClick " + Integer.toString(position), Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void onRefresh() {
        mAdapter.setEmptyView(loadingView);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (mError) {
                    mAdapter.setEmptyView(errorView);
                    mError = false;
                } else {
                    if (mNoData) {
                        mAdapter.setEmptyView(notDataView);
                        mNoData = false;
                    } else {
                        mAdapter.setNewData(DataServer.getSampleDataEx(FIRST_LOAD_DATA_COUNT));
                    }
                }
            }
        }, 1000);
    }
}
